#include <iostream>
#include <ostream>

using std::cout;
using std::endl;

class Complex
{
    friend class Point;
public:
    Complex(double dreal = 0.0, double dimag = 0.0)
    : _dreal(dreal)
    , _dimag(dimag)
    {
        cout << "Complex(double = 0.0, double = 0.0)" << endl;
    }

    ~Complex()
    {
        cout << "~Complex()" << endl;
    }

    friend std::ostream &operator<<(std::ostream &os, const Complex &rhs);
private:
    double _dreal;
    double _dimag;
};

std::ostream &operator<<(std::ostream &os, const Complex &rhs)
{
    if((0 == rhs._dreal) && (0 == rhs._dimag))
    {
        os << 0 << endl;
    }
    else if(0 == rhs._dreal)
    {
        os  << rhs._dimag << "i" << endl;
    }
    else
    {
        os << rhs._dreal;
        if(rhs._dimag > 0)
        {
            os << " + " << rhs._dimag << "i" << endl;
        }
        else if(0 == rhs._dimag)
        {
            os << endl;
        }
        else
        {
            os << " - " << (-1) *rhs._dimag << "i" << endl;
        }
    }

    return os;
}

class Point
{
public:
    /* explicit */ 
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
        cout << "Point(int = 0, int = 0)" << endl;
    }

    Point(const Complex &rhs)
    : _ix(rhs._dreal)
    , _iy(rhs._dimag)
    {
        cout <<"Point(const Complex &)" << endl;
    }

    ~Point()
    {
        cout << "~Point()" << endl;
    }

    friend std::ostream &operator<<(std::ostream &os, const Point &rhs);
private:
    int _ix;
    int _iy;
};

std::ostream &operator<<(std::ostream &os, const Point &rhs)
{
    os << "(" << rhs._ix
       << ", " << rhs._iy
       << ")";

    return os;
}

void test()
{
    Point pt(1, 2);
    cout << "pt = " << pt << endl;

    cout << endl;
    //int---->Point
    //Point(1, 0)
    Point pt2 = 1;//隐式转换, Point(1, 0)
    cout << "pt2 = " << pt2 << endl;

    cout << endl;
    Point pt3 = 11.23;
    cout << "pt3 = " << pt3 << endl;

    cout << endl;
    Point pt4 = Complex(1, 2);
    cout << "pt4 = " << pt4 << endl;
}


int main(int argc, char *argv[])
{
    test();
    return 0;
}

